//
//  NavigationController.m
//  WisdomCommunity
//
//  Created by Yahaong on 16/7/19.
//  Copyright © 2016年 zonten. All rights reserved.
//

#import "NavigationController.h"
#import "UIBarButtonItem+Extension.h"
#import "DeviceControlController.h"
@interface NavigationController ()
<
UIGestureRecognizerDelegate,
UINavigationControllerDelegate
>
@property (nonatomic, getter=isPushing) BOOL pushing;
@end

@implementation NavigationController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    id target = self.interactivePopGestureRecognizer.delegate;
//    
//    SEL handler = NSSelectorFromString(@"handleNavigationTransition:");
    self.delegate = self;
//    UIView *targetView = self.interactivePopGestureRecognizer.view;
//    
//    //  创建pan手势 作用范围是全屏
//    UIPanGestureRecognizer * fullScreenGes = [[UIPanGestureRecognizer alloc]initWithTarget:target action:handler];
//    fullScreenGes.delegate = self;
//    [targetView addGestureRecognizer:fullScreenGes];
//    
//    // 关闭边缘触发手势 防止和原有边缘手势冲突
//    [self.interactivePopGestureRecognizer setEnabled:NO];
}


- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (self.pushing == YES)
    {
        return;
    } else {
        self.pushing = YES;
    }
    if (self.viewControllers.count > 0)
    {
        viewController.hidesBottomBarWhenPushed = YES;
        UIButton *button = [[UIButton alloc]init];
        [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        UIImage *image = nil;
        if ([viewController isKindOfClass:[DeviceControlController class]]) {
            image = [UIImage imageNamed:@"back_white"];
        }else {
            image = [UIImage imageNamed:@"back"];
        }
        [button setImage:image forState:UIControlStateNormal];
        [button setImage:image forState:UIControlStateHighlighted];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button sizeToFit];
        button.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    }
    [super pushViewController:viewController animated:animated];
}
- (void)back
{
    [self popViewControllerAnimated:YES];
}
////  防止导航控制器只有一个rootViewcontroller时触发手势
//- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
//    return self.childViewControllers.count == 1 ? NO : YES;
//}

#pragma mark - UINavigationControllerDelegate
-(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    self.pushing = NO;
}

@end
